package com.cnzha.mr.friend;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;


/**
 * @Author: zhaoxin
 * @email: zdkk@foxmail.com
 * @Date: 2020/4/6 23:11
 */
public class NewFriendRecordWriter extends RecordWriter<Text, Text> {
    FSDataOutputStream fsDataOutputStream = null;

    public NewFriendRecordWriter(TaskAttemptContext context) {
        FileSystem fileSystem;
        try {
            fileSystem = FileSystem.get(context.getConfiguration());
            Path path = new Path("d:/fansList.txt");
            fsDataOutputStream = fileSystem.create(path);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void write(Text key, Text value) throws IOException, InterruptedException {
        fsDataOutputStream.write(key.toString().getBytes());
        fsDataOutputStream.write(value.toString().getBytes());
    }

    @Override
    public void close(TaskAttemptContext context) throws IOException, InterruptedException {
        IOUtils.closeStream(fsDataOutputStream);
    }
}
